package com.itany.corejava.code07_面向对象.继承;

/**
 * @author 石小俊
 * @date 2023年04月19日 14:10
 */
public class Pet {
    String name;
    private int age;

    public Pet(String name) {
        this.name = name;
    }

    public Pet(int age) {
        this.age = age;
    }

    public Pet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void print() {
        System.out.println("我是父类的方法");
    }
}

class Dog extends Pet {
    private String gender;
    String name;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Dog(String name, int age, String gender) {
        // super()只能出现一次,且必须处于第一行
        super(name, age);
        this.gender = gender;
    }

    public void show() {
        // 此时父类与子类中均存在name属性
        // 直接访问默认是子类的name属性
        System.out.println("name:" + name);
        // 如果想要访问父类的,则通过super来调用
        System.out.println("父类name:" + super.name);
        // 遇到这种情况,为了区分
        // 如果访问子类,通过this来调用
        // 如果访问父类,通过super来调用
        System.out.println("子类name:" + this.name);
        super.print();
        this.print();
    }

    public void print() {
        System.out.println("我是子类的方法");
    }

}

class TestPet {
    public static void main(String[] args) {
        Dog dog = new Dog("旺财", 5, "男");
        dog.name = "旺财的儿子";
        dog.show();
    }
}
